﻿(function ($) {
    //备份jquery的ajax方法  
    var _ajax = $.ajax;

    //重写jquery的ajax方法  
    $.ajax = function (opt) {
        //备份opt中error和success方法  
        var fn = {
            success: function (data, textStatus) { }
        }
        if (opt.success) {
            fn.success = opt.success;
        }
        opt.cache = false;
        //扩展增强处理  
        var _opt = $.extend(opt, {
            cache: false,
            success: function (data, textStatus, xhl) {
                if (typeof (data) == "object" && typeof (data.Status) != "undefined" && data.Status == 0) {
                    if ($.alert == undefined) {
                        alert(data.ErrorSimple);
                    } else {
                        $.alert(data.ErrorSimple);
                    }
                } else {
                    fn.success(data, textStatus, xhl);
                }
            }
        });
        _ajax(_opt);
    };
})(jQuery);


//webFileUrl 请求地址
//jsonArgs参数 {}
//callBackFunc 回调 
//是否异步async 默认true
//是否开启遮罩  默认true
$.AjaxServer = function (webFileUrl, jsonArgs, callBackFunc, async, isTip) {
    if (isTip == null) {
        isTip = true;
    }
    try {
        $.ajax({
            url: webFileUrl,
            data: jsonArgs,
            type: 'Post',
            async: async == null ? true : async,
            cache: false,
            success: function (data, textStatus, rqinfo) {
                alert(data);
            },
            error: function () {
                $.alert('提交错误');
            },
            complete: function (XHRequest, T) {
                XHRequest = null
            },
            isTip: isTip
        });
    }
    catch (e) {
        alert(e)
    }
}


/****************全局遮罩******************/
$(document).ajaxSend(ajaxTips).ajaxComplete(unTips);

function ajaxTips(event, xhr, settings) {
    if (settings.isTip != null && settings.isTip == false) {
        return;
    }

    settings._ajaxartDialogClose = false;
    if (typeof (artDialog) != "undefined") {
        settings._ajaxartDialog = artDialog({
            title: false,
            cancel: false,
            fixed: true,
            lock: false,
            height: '5px',
            width: '5px',
            padding: '0px 0px',
            esc: false,
            close: function () {
                return false;
            }
        });
    }
}

function unTips(event, xhr, settings) {
    if (settings._ajaxartDialog != null) {
        settings._ajaxartDialog.config.close = function () { return true };
        settings._ajaxartDialog.close(); // .time(1);
    }
}


/****************晚绑定******************/
//dataType：Confim | DialogAjax
//dataHref: Ajax提交的地址
//alt：对话框提示文字或者弹出窗口的title
//dataReturn：完成以后的回调
$.DataTypeBind = function () {
    $("[dataType]").each(function () {

        //标记是否已经绑定
        if ($(this).attr("DataTypeClick") == "True") {
            $(this).unbind('click');
        } else {
            $(this).attr("DataTypeClick", "True")
        }

        $(this).bind("click", function () {
            try {
                col = $(this);

                //完成的回调
                var funobj = true;
                if (col.attr("dataReturn") != undefined) {
                    funobj = eval(col.attr("datareturn"));
                }

                var href = $(obj).attr("dataHref");
                var alt = $(obj).attr("alt");

                if (col.attr("dataType") == "confim") {
                    $.ConfimSubmit(col, href, alt, funobj);
                } else {
                    $.DialogAjax(col, href, alt, funobj);
                }
            }
            catch (e) {

            }
            return false;
        })
    })
}

//提示是否提交指定的链接
//传入控件对象obj
$.ConfimSubmit = function (col, href, alt, funobj) 
{
    $.confim(alt, function () {
        $.AjaxServer(href, null, function (data) {
            if (typeof (funobj) == "function") {
                funobj(data);
            }
        });
    });
       
}

//在弹窗中显示内容
//传入控件对象obj
$.DialogAjax = function (col, href, alt, funobj) 
{
   $.AjaxServer(href, null, function (data) {
            var id = newGuid();
            art.dialog({
                title: alt,
                id: id,
                fixed: false,
                lock: false,
                content: "<span style='line-height:22px;'>" + _SetDialogContent(data, id) + "</span>",
                ok: false,
                close: funobj
            });
   });
}

/********************对话框****************************/

//提示对话框
//content：显示内容  callback：完成回调
$.alert = function (content, callback) {
    var id = newGuid();
    return art.dialog({
        title: '警告',
        id: id,
        icon: "warning",
        fixed: true,
        lock: true,
        content: "<span style='line-height:22px;'>" + _SetDialogContent(content, id) + "</span>",
        ok: true,
        close: callback
    });
};

//确认提示
//content：显示内容  yesCallback：选择是调用 noCallback：选择否调用
$.confim = function (content, yesCallback, noCallback) {
    var id = newGuid();
    return art.dialog({
        title: '确认',
        id: id,
        icon: "question",
        fixed: true,
        lock: true,
        content: "<span style='line-height:22px;'>" + _SetDialogContent(content, id) + "</span>",
        resize: false,
        ok: yesCallback == null ? true : yesCallback,
        cancel: noCallback == null ? true : noCallback
    });
};

//短暂显示
// content：显示内容  follow：跟随控件 time：显示时间
$.tips = function (content, follow, time, funCall) {

    if (funCall == null) {
        funCall = function () { };
    }

    var id = newGuid();
    return art.dialog({
        id: id,
        title: false,
        cancel: false,
        fixed: true,
        lock: false,
        height: '20px',
        follow: document.getElementById(follow),
        close: funCall
    })
    .content('<div style="padding: 0 1em;">' + _SetDialogContent(content, id) + '</div>')
    .time(time || 1.5);
};


//生成对话框ID
function newGuid() {
    var guid = "";
    for (var i = 1; i <= 32; i++) {
        var n = Math.floor(Math.random() * 16.0).toString(16);
        guid += n;
        if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
            guid += "-";
    }
    return guid;
}

//当前对话框
function getCurrentDialog(elm) {
    var values = $(elm).parents("[_dialogId]");
    var _dialogId = values.attr("_dialogId");
    var dialog = art.dialog.list[_dialogId];
    return dialog;
}


function _SetDialogContent(content, id) {
    return "<div _dialogId='" + id + "'  >" + content + "</div>";
}



/*******************其他********************/

function documentCheck() {
    ///<summary>
    /// 用于在通过AJAX的形式取得新的页面内容之后,重新刷新页面信息以便实现验证
    ///</summary>

    $.validator.unobtrusive.parse(document);
}


//数组删除元素 传入数组下标
Array.prototype.remove = function (dx) {
    if (isNaN(dx) || dx > this.length) { return false; }
    this.splice(dx, 1);
    return this;
}


//用于BooleanType控件切换是否
function BooleanTypeChange(obj) {
    $(obj).val($(obj).is(':checked') ? "1" : "0");
}

//判断浏览器是否是ie6
function isie6() {
    if ($.browser.msie) {
        if ($.browser.version == "6.0") return true;
    }
    return false;
}


//绑定所有IsPurview的a标签，用于判断该标签是否有权限访问，IsPurview='/home/home/index'
//pwUrl表示请求验证的路径
function PurviewBind() {

    $("[IsPurview]").each(function (index, domEle) {
        var pw = $(this).attr("IsPurview").split('/');
        $(this).bind("click", function () {
            var b = false;

            $.AjaxServer(pwUrl, { action: pw[3], controller: pw[2], area: pw[1] }, function (obj) {
                if (obj.Status == 1) {
                    b = true;
                } else {
                    $.tips(obj.ErrorSimple);
                }
            }, false);

            return b;
        });
    });

}

//隔行样式
function tab_wrapBind() {
    $(".table tr:even").addClass("tr_bg");
}

/************分页**************/

//AjaxPagerX 分页控件跳转用于跳转 传入form名称和页码
function AjaxToPage(formname, pageindex) {

    var _pageIndex = '#' + formname + ' #pageIndex';
    $(_pageIndex).val(pageindex);

    if ($(_pageIndex).length == 0) {
        for (var i = 0; i < document.getElementsByName("pageIndex").length; i++) {
            document.getElementsByName("pageIndex")[i].value = pageindex;
        }
    }

    $('#' + formname).submit();
    return false;
}

//AjaxPagerX 分页控件跳转用于刷新当前页      
function AjaxToRefresh(formname) {
    var _pageIndex = '#' + formname + ' #pageIndex';
    var _HpageIndex = '#' + formname + ' #HpageIndex';
    $(_pageIndex).val($(_HpageIndex).val());
    $('#' + formname).submit();
    return false;
}

//AjaxPagerX 分页控件用于改变form里面隐藏域的值，如果没有将创建一个
function UpdateHidden(formname, hiddenName, value) {
    var temp_hiddenName = '#' + formname + ' #' + hiddenName;

    if ($(temp_hiddenName).length > 0 || (isie6() && ($("#" + formname + " input[name='" + hiddenName + "']").length > 0))) {
        $(temp_hiddenName).val(value);
    }
    else {
        var str = '<input  type=\"hidden\" id=\"' + hiddenName + '\" name=\"' + hiddenName + '\" value=\"' + value + '\"  /> ';
        $('#' + formname).append(str);
    }
}


//*******初始化********//
$(function () {

    yqinfo();
})

function yqinfo() 
{
    PurviewBind();
    $.DataTypeBind();
    documentCheck();
    tab_wrapBind();
}